package com.yc.fav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.fav.Utils;
import com.yc.fav.entity.User;
import com.yc.fav.mapper.UserMapper;
import com.yc.fav.service.BizException;
import com.yc.fav.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2024-08-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void reg(User user) throws BizException {
        //判断字段是否为空
        Utils.checkEmpty(user.getName(),"用户名不能为空");
        Utils.checkEmpty(user.getPwd(),"密码不能为空");
        Utils.checkEmpty(user.getEmail(),"邮箱不能为空");
        Utils.checkEmpty(user.getHead(),"头像不能为空");
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getEmail,user.getEmail());
        User user1 = userMapper.selectOne(lqw);
        if(user1 != null){
            Utils.check(true,"该邮箱已注册");
            return;
        }
        boolean save = this.save(user);
        if (save) {
            return;
        }else {
            Utils.check(true,"添加失败");
        }
    }
}
